package com.easepal7506a.project.ui.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.observer.TimeObserver;
import com.easepal7506a.project.ui.iview.IAttitudeAdjustmentView7506;
import com.easepal7506a.project.ui.presenter.AttitudeAdjustmentPressenter7506;
import com.easepal7506a.project.utils.ActivityUtils;
import com.example.reslib.utils.PreferenceWrapper;
import com.ogawa.base.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 姿态调节
 */
public class AttitudeAdjustmentActivity7506 extends BaseActivity implements View.OnClickListener, IAttitudeAdjustmentView7506, TimeObserver.onTimeUpdataLis {
    private LinearLayout mLlReset;
    private LinearLayout mLlZero1;
    private LinearLayout mLlZero2;
    private ImageView mIvReset;
    private ImageView mIvZero1;
    private ImageView mIvZero2;
    private List<TextView> mIvAdjustViews = new ArrayList<>();
    private String mZero;
    private AttitudeAdjustmentPressenter7506 mPresenter;
    private TextView chairup;//升背
    private TextView chairdown; //降背
    private TextView chair_hand_up; //缩腿
    private TextView cahir_leg_down;//伸腿
    private TextView chair_foot_down; //降腿
    private TextView chair_foot_up;//升腿
    private ImageView mIvChair;

    @Override
    public int setLayoutId() {
        return R.layout.activity_attitude_adjustment_7506;
    }


    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setGreenBackBotton();
        titleBar.setTitle("姿态调节");
        setBackGround(R.mipmap.bg);
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
    }

    private void initView() {
        chairup = findViewById(R.id.chairup);
        chairdown = findViewById(R.id.chairdown);
        chair_hand_up = findViewById(R.id.chair_hand_up);
        cahir_leg_down = findViewById(R.id.cahir_leg_down);
        chair_foot_down = findViewById(R.id.chair_foot_down);
        chair_foot_up = findViewById(R.id.chair_foot_up);
        mIvChair = findViewById(R.id.chair);
        PreferenceWrapper mSp = new PreferenceWrapper();
        int mChoose = Integer.parseInt(mSp.getStringValue("app", "1"));
        if (DataManager.getInst().isIs7506L()) {
            mIvChair.setSelected(true);
            if (mChoose == 4) {
                mIvChair.setImageResource(R.mipmap.img_chair_7507);
            }
        }
        mIvAdjustViews.add(chair_foot_up);
        mIvAdjustViews.add(chair_foot_down);
        mIvAdjustViews.add(cahir_leg_down);
        mIvAdjustViews.add(chair_hand_up);
        mIvAdjustViews.add(chairdown);
        mIvAdjustViews.add(chairup);
        mIvReset = findViewById(R.id.iv_reset);
        mIvZero1 = findViewById(R.id.iv_zero1);
        mIvZero2 = findViewById(R.id.iv_zero2);
        mLlReset = findViewById(R.id.ll_reset);
        mLlZero1 = findViewById(R.id.ll_zero1);
        mLlZero2 = findViewById(R.id.ll_zero2);
        mLlReset.setOnClickListener(this);
        mLlZero1.setOnClickListener(this);
        mLlZero2.setOnClickListener(this);
        chairup.setOnClickListener(this);
        chairdown.setOnClickListener(this);
        chair_hand_up.setOnClickListener(this);
        cahir_leg_down.setOnClickListener(this);
        chair_foot_down.setOnClickListener(this);
        chair_foot_up.setOnClickListener(this);
        mZero = DataManager.getInst().getValue(CommmandNum.mStatusZERO);
        setZero(mZero);
        mPresenter = new AttitudeAdjustmentPressenter7506(this);
        TimeObserver.getInst().registerObserver(this);

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.chairup) {
            sendCommond(CommmandNum.SHENBEI);

        } else if (i == R.id.chairdown) {
            sendCommond(CommmandNum.DAOBEI);

        } else if (i == R.id.chair_hand_up) {
            sendCommond(CommmandNum.SUOTUI);

        } else if (i == R.id.chair_foot_down) {
            sendCommond(CommmandNum.JIANGTUI);

        } else if (i == R.id.chair_foot_up) {
            sendCommond(CommmandNum.TAITUI);

        } else if (i == R.id.cahir_leg_down) {
            sendCommond(CommmandNum.SHENTUI);

        } else if (i == R.id.ll_reset) {
            sendCommond(CommmandNum.FUWEI);

        } else if (i == R.id.ll_zero1) {
            sendCommond(CommmandNum.LINZL1);

        } else if (i == R.id.ll_zero2) {
            sendCommond(CommmandNum.LINZL2);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestory();
        TimeObserver.getInst().removeObserver(this);
    }

    public void setZero(String zero) {
        if (zero.equals("0")) {
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(false);
        } else if (zero.equals("1")) {
            mIvZero1.setSelected(true);
            mIvZero2.setSelected(false);
        } else if (zero.equals("2")) {
            mIvZero1.setSelected(false);
            mIvZero2.setSelected(true);
        }


    }


    @Override
    public void getZero(String zero) {
        setZero(zero);
    }

    @Override
    public void getReset(String reset) {
        mIvReset.setSelected(reset.equals("1"));
    }


    @Override
    public void getUpDownLeg(String value) {
        if (value.equals("1")) {
            chair_foot_up.setSelected(true);
            chair_foot_down.setSelected(false);
        } else if (value.equals("2")) {
            chair_foot_up.setSelected(false);
            chair_foot_down.setSelected(true);
        } else {
            chair_foot_up.setSelected(false);
            chair_foot_down.setSelected(false);
        }
    }

    @Override
    public void getShenSuo(String value) {
        if (value.equals("1")) {
            cahir_leg_down.setSelected(true);
            chair_hand_up.setSelected(false);
        } else if (value.equals("2")) {
            cahir_leg_down.setSelected(false);
            chair_hand_up.setSelected(true);
        } else {
            cahir_leg_down.setSelected(false);
            chair_hand_up.setSelected(false);
        }
    }

    @Override
    public void getBackDown(String value) {
        if (value.equals("1")) {
            chairdown.setSelected(true);
            chairup.setSelected(false);
        } else if (value.equals("2")) {
            chairdown.setSelected(false);
            chairup.setSelected(true);
        } else {
            chairdown.setSelected(false);
            chairup.setSelected(false);
        }
    }

    @Override
    public void isPause(boolean isPause) {
        if (isPause) {
            pauseTime();
        } else {
            reStarTime();
        }
    }

    @Override
    public void onupdataTime(String min, String sec) {
        doUpDataTime(min, sec);
    }

    @Override
    public void timeFinish() {
        super.timeFinish();
        if (CommonUtil.isActivityTop(AttitudeAdjustmentActivity7506.class, this)) {
            showFinishDialog(this, "0", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancleFinishDialog();
                    ActivityUtils.startHomeActivity(AttitudeAdjustmentActivity7506.this);
                    AttitudeAdjustmentActivity7506.this.finish();
                }
            });
        }
    }
}
